home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlrun / adlrun.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  469 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3.  
  4. #include "adltypes.h"
  5. #include "adlprog.h"
  6. #include "adlrun.h"
  7. #include "adldef.h"
  8. #include "virtmem.h"
  9. #include "vstring.h"
  10.  
  11. struct    pagetab
  12.     codetab;
  13. struct    actrec
  14.     actlist[ 10 ];
  15. address
  16.     ip;
  17. int16
  18.     stack[ STACKSIZE ],
  19.     sp,
  20.     bp,
  21.     numact,
  22.     curract,
  23.     Verb,
  24.     Prep,
  25.     Iobj,
  26.     Inoun,
  27.     Imod,
  28.     NumDobj,
  29.     Conj[ NUMDO ],
  30.     Dnoun[ NUMDO ],
  31.     Dobj[ NUMDO ],
  32.     Dmod[ NUMDO ],
  33.     vecverb[ 10 ],
  34.     demons[ 10 ],
  35.     fuses[ 10 ],
  36.     ftimes[ 10 ],
  37.     f_actors[ 10 ],
  38.     numd,
  39.     numf,
  40.     currturn,
  41.     prompter,
  42.     wordwrite,
  43.     numsave,
  44.     restarted,
  45.     debug,
  46.     header,
  47. #if AMIGA
  48.     scrwidth = 70,
  49. #else
  50.     scrwidth = 79,
  51. #endif
  52.     numcol,
  53.     Phase,
  54.     /* The following 6 vars are not actually used in adlrun,
  55.        but we need to define them for the loader. */
  56.     NUM_VARS,
  57.     NUM_ROUTS,
  58.     NUM_OBJS,
  59.     NUM_VERBS,
  60.     NUM_PREP;
  61. char
  62.     *inname = "adlcomp.out",
  63.     *r_name = "\0",
  64.     *USAGE = "Usage: adlrun [filename] [-d] [-h] [-r savefile]\n",
  65.     *H_STR = "%-48sScore: %-4d  Moves: %-4d\n",
  66.     tempc[ 4096 ],        /* Temporary strings */
  67.     savec[ 1024 ];        /* "Permanent" strings */
  68. int
  69.     infile = -1;
  70. FILE
  71. #if MULTIPLEX
  72.     *CURRTTY,            /* Current output file */
  73.     *SYSTTY,            /* Default output file */
  74. #endif
  75.     *wordfile = (FILE *)0,
  76.     *scriptfile = (FILE *)0;
  77.  
  78. struct exit_place
  79.     exits[ 5 ];
  80.  
  81. #if MSDOS
  82.     extern char
  83.         *getml();
  84. #    define calloc( size, num )    getml( (long)(size * num) )
  85. #else
  86.     extern char
  87.         *calloc();
  88. #endif
  89.  
  90. #define    islegal(c)    (c&&(c!=' ')&&(c!='\t')&&(c!='\n')&&(c!='-'))
  91.  
  92.  
  93. main( argc, argv )
  94. int    argc;
  95. char    *argv[];
  96. {
  97.     getadlargs( argc, argv );
  98.     while( 1 )
  99.     if( SET_EXIT( 4 ) == 0 )
  100.         break;
  101.     if( restarted )
  102.     init( 0 );
  103.     else
  104.     init( 1 );
  105.     driver();
  106. }
  107.  
  108.  
  109. getadlargs( argc, argv )
  110. int
  111.     argc;
  112. char
  113.     *argv[];
  114. {  
  115.     int
  116.     i;
  117.     char
  118.     *getnext();
  119.  
  120.     for( i = 1; i < argc; i++ ) {
  121.     if( *argv[ i ] == '-' )
  122.         switch( *++argv[ i ] ) {
  123.         case 'd' :
  124.             debug = 1;
  125.             break;
  126.         case 'h' :
  127.             header = 1;
  128.             break;
  129.         case 'r' :
  130.             r_name = getnext( &i, argv );
  131.             break;
  132.         default  :
  133.             fputs( USAGE, stderr );
  134.             exit( -1 );
  135.         }
  136.     else if( !strcmp( inname, "adlcomp.out" ) )
  137.         inname = argv[ i ];
  138.     else {
  139.         fputs( USAGE, stderr );
  140.         exit( -1 );
  141.     }
  142.     }
  143. }
  144.  
  145.  
  146. init( first )
  147. int16
  148.     first;
  149. {
  150.     int16
  151.     me,
  152.     *temp;
  153.  
  154.     if( first ) {
  155.     insertkey( ",",        COMMA,    0, 0 );
  156.     insertkey( "and",    CONJ,    0, 0 );
  157.     insertkey( "but",    CONJ,    1, 0 );
  158.     insertkey( ".",        SEP,    0, 0 );
  159.     insertkey( "!",        SEP,    0, 0 );
  160.     insertkey( "?",        SEP,    0, 0 );
  161.     insertkey( "then",    SEP,    0, 0 );
  162.  
  163.     SRAND( rand_seed() );
  164.     if( (infile = open( inname, RB )) < 0 ) {
  165.         fprintf( stderr, "Error opening file %s\n", inname );
  166.         exit( -1 );
  167.     }
  168.  
  169. #if MULTIPLEX
  170.     SYSTTY = fopen( "/dev/tty", "r+" );
  171.     if( SYSTTY == (FILE *)0 ) {
  172.         fprintf( stderr, "Unable to open output device.\n" );
  173.         exit( -1 );
  174.     }
  175.     CURRTTY = SYSTTY;
  176. #endif
  177.  
  178.     head_setup();
  179.     sayer( "ADL interpreter - Version 3.2 - June 7, 1987\n" );
  180.     sayer( "Copyright 1985, 1986, 1987 by Ross Cunniff\n" );
  181.     sayer( "All rights reserved.\n" );
  182.  
  183.     /* Read in the input file */
  184.     lseek( infile, 0L, 0 );
  185.     read( infile, &hdr, sizeof( struct header ) );
  186.     if( hdr.magic != M_ADL ) {
  187.         fprintf( stderr, "%s: Not an ADL datafile.\n", inname );
  188.         head_term();
  189.         exit( -1 );
  190.     }
  191.  
  192.     /* Read the arrays */
  193.  
  194.     read_symtab( infile, &hdr.symindex );
  195.     loadarray( (char **)&routspace,    &hdr.routindex, 1 );
  196.     loadarray( (char **)&str_tab,    &hdr.strtabindex, 1 );
  197.     loadarray( (char **)&prepspace,    &hdr.prepindex, 1 );
  198.     loadarray( (char **)&verbsyn,    &hdr.vsindex, 1 );
  199.     loadarray( (char **)&nounspace,    &hdr.nounindex, 1 );
  200.  
  201.     /* Initialize virtual code and string stuff */
  202.     vsinit( infile, hdr.strindex.ptr, 1, tempc, savec, &numsave, str_tab );
  203.     vm_init( infile, hdr.codeindex.ptr, &codetab );
  204.  
  205.     }    /* if( first ) */
  206.  
  207.     temp = stack;
  208.     loadarray( (char **)&temp,        &hdr.varindex, 0 );
  209.     loadarray( (char **)&verbspace,    &hdr.verbindex, first );
  210.     loadarray( (char **)&objspace,    &hdr.objindex, first );
  211.  
  212.     if( scriptfile != (FILE *)0 ) {
  213.     fclose( scriptfile );
  214.     scriptfile = (FILE *)0;
  215.     }
  216.  
  217.     if( wordwrite )
  218.     fclose( wordfile );
  219.  
  220.     /* Initialize some variables (This is for ($spec 2) -> restart) */
  221.     sp = bp = NUMVAR;
  222.     ip = curract = numd = numf = 0;
  223.     wordwrite = numsave = prompter = currturn = 0;
  224.  
  225.     actlist[ 0 ].linebuf = actlist[ 0 ].savebuf;
  226.     *actlist[ 0 ].linebuf = '\0';
  227.     actlist[ 0 ].interact = 1;
  228.     numact = 0;
  229.  
  230.     clearmacro();
  231.  
  232.     if( *r_name ) {
  233.     restoregame( r_name );
  234.     r_name = "\0";
  235.     }
  236.     else
  237.     callone( _START );
  238.  
  239.     restarted = 0;
  240. }
  241.  
  242.  
  243. driver()
  244. {
  245.     int
  246.     i;
  247.  
  248.     /* Outermost adlrun loop (never exited except to quit adlrun) */
  249.     while( 1 ) {
  250.  
  251.     vsflush();    /* Clear out the temporary strings */
  252.     curract = 0;
  253.  
  254.     /* Daemon loop */
  255.     for( curract = numact - 1; curract >= 0; curract-- ) {
  256. #if MULTIPLEX
  257.         if( actrec[ curract ].ttyfile == (FILE *)0 )
  258.         CURRTTY = SYSTTY;
  259.         else
  260.         CURRTTY = actrec[ curract ].ttyfile;
  261. #endif
  262.         Phase = 0;
  263.         if( SET_EXIT( 1 ) == 0 ) {
  264.         if( SET_EXIT( 0 ) == 0 )
  265.             execdems();
  266.         }
  267.     }
  268.     CLR_EXIT( 0 );
  269.  
  270.     /* Main actor loop */
  271.     for( curract = numact - 1; curract >= 0; curract-- ) {
  272. #if MULTIPLEX
  273.         if( actlist[ curract ].ttyfile == (FILE *)0 )
  274.         CURRTTY = SYSTTY;
  275.         else
  276.         CURRTTY = actlist[ curract ].ttyfile;
  277. #endif
  278.  
  279.         if( SET_EXIT( 1 ) != 0 )
  280.         continue;
  281.  
  282.         /* Delete actor if not interactive and line buffer is empty */
  283.         if( (*PSTRING == 0) && (INTERACT == 0) ) {
  284.         delactor( CURRACT );
  285.         continue;        /* with for loop */
  286.         }
  287.  
  288.         /* Read and parse the user's input */
  289.         initvars();
  290.         while( 1 )
  291.         if( SET_EXIT( 3 ) == 0 )
  292.             break;
  293.         if( get_buffer() < 0 )
  294.         continue;
  295.  
  296.         /* Call the appropriate routines */
  297.         callrouts();
  298.  
  299.         CLR_EXIT( 3 );
  300.     }    /* for( curract ... ) */
  301.     }        /* outer while( 1 ) */
  302. }
  303.  
  304.  
  305. char *
  306. getnext( which, argv )
  307. int
  308.     *which;
  309. char
  310.     *argv[];
  311. {
  312.     if( *++argv[ *which ] )
  313.     return argv[ *which ];
  314.     else
  315.     return argv[ ++*which ];
  316. }
  317.  
  318.  
  319. rand_seed()
  320. {
  321.     long
  322.     time();
  323.  
  324.     SRAND( (int)time( (long *)0 ) );
  325. }
  326.  
  327. sayer( s )
  328. char
  329.     *s;
  330. {
  331.     char
  332.     word[ 80 ];
  333.  
  334.     while( *s ) {
  335.     getchunk( &s, word );
  336.     if( *word == '\n' ) {
  337.         crlf();
  338.         continue;
  339.     }
  340.     else if( (numcol + strlen( word )) > scrwidth ) {
  341.         while( *word == ' ' )    /* Eat up blanks */
  342.         getchunk( &s, word );
  343.         if( !*word ) {
  344.         fflush( stdout );
  345.         return;            /* The string was blank terminated */
  346.         }
  347.         crlf();
  348.     }
  349.     numcol += strlen( word );
  350. #if MULTIPLEX
  351.     fputs( word, CURRTTY );
  352.     fflush( CURRTTY );
  353. #else
  354.     fputs( word, stdout );
  355.     fflush( stdout );
  356. #endif
  357.     if( scriptfile != (FILE *)0 )
  358.         fputs( word, scriptfile );
  359.     }
  360. #if MULTIPLEX
  361.     fflush( CURRTTY );
  362. #else
  363.     fflush( stdout );
  364. #endif
  365. }
  366.  
  367.  
  368. /* VARARGS */
  369. loadarray( a, d, first )
  370. char
  371.     **a;
  372. struct    adldir
  373.     *d;
  374. int16
  375.     first;
  376. {
  377.     if( first && (d->numobjs * d->objsize) ) {
  378.     *a = calloc( d->numobjs, d->objsize );
  379.     if( *a == (char *)0 )
  380.         error( 27 );        /* Out of memory */
  381.     }
  382.     lseek( infile, d->ptr, 0 );
  383.     read( infile, *a, d->numobjs * d->objsize );
  384. }
  385.  
  386.  
  387. getchunk( s, word )
  388. char
  389.     **s,
  390.     *word;
  391. {
  392.     if( !**s ) {
  393.     *word = '\0';
  394.     return;
  395.     }
  396.     if( islegal( **s ) )
  397.     while( islegal( **s ) )
  398.         *word++ = *((*s)++);
  399.     else if( **s == '\t' ) {            /* Expand the tab */
  400.     strcpy( word, "        " );
  401.     word[ 8 - (numcol % 8) ] = '\0';
  402.     (*s)++;
  403.     return;
  404.     }
  405.     else
  406.     *word++ = *((*s)++);
  407.     *word = '\0';
  408. }
  409.  
  410.  
  411. crlf()
  412. {
  413. #if MULTIPLEX
  414.     putc( '\n', CURRTTY );
  415. #else
  416.     putchar( '\n' );
  417. #endif
  418.     if( scriptfile != (FILE *)0 )
  419.     putc( '\n', scriptfile );
  420.     numcol = 0;
  421. }
  422.  
  423.  
  424. get_buffer()
  425. {
  426.     /* Loop for user input */
  427.     while( 1 ) {
  428.  
  429.     /* Get a non-empty line */
  430.     if( !*PSTRING ) {
  431.         PSTRING = &SAVEBUF[ 0 ];
  432.         u_prompt();
  433.         getstring( PSTRING );
  434.     }
  435.  
  436.     /* Parse the string */
  437.     if( !parse() ) {
  438.         *PSTRING = '\0';
  439.         initvars();        /* Failed parse; init the variables    */
  440.         if( INTERACT )
  441.         continue;    /* with While (interactive actor, try again) */
  442.         else {
  443.         delactor( CURRACT );
  444.             return -1;        /* from While (the actor needs to die) */
  445.         }
  446.     }
  447.     else
  448.         break;        /* The parse was successful */
  449.     }
  450.     return 0;
  451. }
  452.  
  453.  
  454. int16
  455. noun_exists( adj, noun )
  456. int16
  457.     adj, noun;
  458. {
  459.     int16
  460.     t;
  461.  
  462.     for( t = nounspace[ noun ]; t != 0; t = objspace[ t ].others )
  463.     if( objspace[ t ].adj == adj )
  464.         return t;
  465.     return -1;
  466. }
  467.  
  468. /*** EOF adlrun.c ***/
  469.